package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.MenuDao;
import com.djinfo.work.dao.RoleDao;
import com.djinfo.work.dao.RoleMenuDao;
import com.djinfo.work.dao.RoleUserDao;
import com.djinfo.work.entity.TbSysMenu;
import com.djinfo.work.entity.TbSysRoleMenu;
import com.djinfo.work.entity.TbSysRoles;
import com.djinfo.work.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 */
@Service("RoleService")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RoleMenuDao roleMenuDao;
	@Autowired
	private RoleUserDao roleUserDao;
    @Autowired
    private MenuDao menuDao;

	@Override
	public void save(TbSysRoles tbSysRoles) {
		roleDao.save(tbSysRoles);
	}

	@Override
	public void saveMenuIdsToRole(TbSysRoleMenu tbSysRoleMenu) {
		roleMenuDao.save(tbSysRoleMenu);
	}

	@Override
	public void deleteMenuByRoleId(String RoleId) {
		roleMenuDao.deleteMenuByRoleId(RoleId);
	}

	@Override
	public void update(TbSysRoles tbSysRoles) {
		roleDao.update(tbSysRoles);
	}

	@Override
	public void delete(TbSysRoles tbSysRoles) {
		roleDao.delete(tbSysRoles);
	}

	@Override
	public TbSysRoles selectOne(TbSysRoles tbSysRoles) {
        return roleDao.selectOne(tbSysRoles);
	}

	@Override
	public List<TbSysRoles> page(TbSysRoles tbSysRoles, Integer pageNum, Integer pageSize) {
		return roleDao.page(tbSysRoles,  pageNum,  pageSize);
	}

	@Override
	public Integer getAllRowCount() {
		return roleDao.getAllRowCount();
	}

	@Override
	public List getMenuList(String roleId) {
		return roleDao.getMenuList(roleId);
	}

	@Override
	public List getRoleList() {
		return roleDao.getRoleList();
	}

	@Override
	public List getRoleList2(String uid) {
		return roleDao.getRoleList2(uid);
	}

	@Override
	public void deleteUserRoleByRoleId(String roleId) {
		roleUserDao.deleteUserRoleByRoleId(roleId);
	}

	@Override
	public AjaxJson list(TbSysRoles tbSysRoles, Integer pagenum, Integer pagesize) {
		HashMap<String, Object> map = new HashMap<>();
		AjaxJson ajaxJson = new AjaxJson();
		try {
			List<TbSysRoles> res = roleDao.page(tbSysRoles,pagenum,pagesize);
			Integer count = roleDao.getAllRowCount();
			map.put("dataList",res);
			map.put("total",count);
			ajaxJson.setResult(map);
		} catch (Exception e) {
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson oneUserRoleList(String uid) {
		HashMap map=new HashMap();
		AjaxJson ajaxJson = new AjaxJson();
		try {
			List list=roleDao.getRoleList2(uid);
			map.put("dataList",list);
			ajaxJson.setResult(map);
		} catch (Exception e) {
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson allList() {
		HashMap<String, Object> map = new HashMap<>();
		AjaxJson ajaxJson = new AjaxJson();
		List<TbSysRoles> list=roleDao.getRoleList();
		map.put("roles",list);
		ajaxJson.setResult(map);
		return ajaxJson;
	}

    @Override
    public AjaxJson info(TbSysRoles tbSysRoles) {
        List list=new ArrayList();
        List list1=new ArrayList();
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        try {
            TbSysRoles res = roleDao.selectOne(tbSysRoles);
            if(res!=null) {
                List<String> menuIds = roleDao.getMenuList(res.getPkRoleId());
                for (String id : menuIds) {
                    TbSysMenu tbSysMenu = menuDao.getMenuById(id);
                    list1.add(tbSysMenu);
                }
            }
            TbSysMenu menu=new TbSysMenu();
            menu.setParentAuthId("0");
            List<TbSysMenu> allParentMenu = menuDao.getLevel1List(menu);
            List<TbSysMenu> allMenuList = menuDao.getAllMenuList();
            for(TbSysMenu tbSysMenu :allParentMenu){
                list.add(tbSysMenu);
                addLeafMenuToList(list,tbSysMenu,allMenuList);
            }
            map.put("all",list);
            map.put("some",list1);
            ajaxJson.setResult(map);
        } catch (Exception e) {
            ajaxJson.setCode(301);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    public void addLeafMenuToList(List list,TbSysMenu parentMenu,List<TbSysMenu> allMenuList){
        for (TbSysMenu temp:allMenuList) {
            if(temp.getParentAuthId().equals(parentMenu.getPkMenuId())){
                list.add(temp);
                if(!temp.getIsLeaf().equals("1")){
                    addLeafMenuToList(list,temp,allMenuList);
                }
            }
        }
    }

    @Override
    public AjaxJson save(TbSysRoles tbSysRoles, String[] roles, HttpServletRequest request) {
        String userId=(String) request.getSession().getAttribute("userId");
        String nowTime= StringUtil.getNowTime();
        AjaxJson ajaxJson = new AjaxJson();
        if(tbSysRoles.getPkRoleId()==null||"".equals(tbSysRoles.getPkRoleId())){
            tbSysRoles.setPkRoleId(null);
            tbSysRoles.setCreateUser(userId);
            tbSysRoles.setUpdateUser(userId);
            tbSysRoles.setCreateTime(nowTime);
            tbSysRoles.setUpdateTime(nowTime);
            roleDao.save(tbSysRoles);
            for(String id:roles){
                TbSysMenu tbSysMenu =menuDao.getMenuById(id);
                TbSysRoleMenu tbSysRoleMenu =new TbSysRoleMenu();
                tbSysRoleMenu.setMenuId(id);
                tbSysRoleMenu.setRoleId(tbSysRoles.getPkRoleId());
                tbSysRoleMenu.setCreateTime(nowTime);
                tbSysRoleMenu.setUpdateTime(nowTime);
                tbSysRoleMenu.setUpdateUser(userId);
                tbSysRoleMenu.setCreateUser(userId);
                roleMenuDao.save(tbSysRoleMenu);
            }
        }else{
            tbSysRoles.setUpdateTime(nowTime);
            tbSysRoles.setUpdateUser(userId);
            roleDao.update(tbSysRoles);
            roleMenuDao.deleteMenuByRoleId(tbSysRoles.getPkRoleId());
            for(String id:roles){
                TbSysMenu tbSysMenu =menuDao.getMenuById(id);
                TbSysRoleMenu tbSysRoleMenu =new TbSysRoleMenu();
                tbSysRoleMenu.setMenuId(id);
                tbSysRoleMenu.setRoleId(tbSysRoles.getPkRoleId());
                tbSysRoleMenu.setCreateTime(nowTime);
                tbSysRoleMenu.setUpdateTime(nowTime);
                tbSysRoleMenu.setUpdateUser(userId);
                tbSysRoleMenu.setCreateUser(userId);
                roleMenuDao.save(tbSysRoleMenu);
            }
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson delete(String roleId) {
        TbSysRoles tbSysRoles=new TbSysRoles();
        AjaxJson ajaxJson = new AjaxJson();
        try {
            tbSysRoles =new TbSysRoles();
            tbSysRoles.setPkRoleId(roleId);
            tbSysRoles =roleDao.selectOne(tbSysRoles);
            roleDao.delete(tbSysRoles);
            roleUserDao.deleteUserRoleByRoleId(roleId);
            roleMenuDao.deleteMenuByRoleId(roleId);
        } catch (Exception e) {
            ajaxJson.setCode(200);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }


}
